package cn.itcast.service.impl;


import cn.itcast.mapper.WeatherMapper;
import cn.itcast.pojo.Weather;

import cn.itcast.service.WeatherService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WeatherServiceImpl extends ServiceImpl<WeatherMapper, Weather> implements WeatherService {
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public void saveWeather(String cityName) {
        String url = "http://weather-service/weather/getCityWeatherInfo/" + cityName;
        String json = restTemplate.getForObject(url, String.class);
        List<Weather> list = JSON.parseArray(json, Weather.class);
        if(list != null && !list.isEmpty()) {
            Weather weather = list.get(0);
            save(weather);
        }
    }
}
